package cn.qffj.memo.controller;


import cn.qffj.memo.domain.TextContent;
import cn.qffj.memo.domain.User;
import cn.qffj.memo.service.UserService;
import cn.qffj.memo.utlis.ResultVOUtils;
import cn.qffj.memo.vo.RestfulVO;
import cn.qffj.memo.vo.TextContentVO;
import cn.qffj.memo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("user")
public class UserResultController {

    @Autowired
    private UserService userService;

    //注册将个人信息存放到数据库中
    @PostMapping(value = "login")
    public RestfulVO<User> CreateData(User user) {
        //判断用户名是否存在
        boolean str = userService.existsByUserName(user.getUserName());

        if (str != true) {

            user.setUserName(user.getUserName());
            user.setAccount(user.getAccount());
            user.setPassword(user.getPassword());
            user.setIssue(user.getIssue());
            user.setIssueResult(user.getIssueResult());
            User user1 = userService.save(user);

            return ResultVOUtils.login(user1);
        } else

        {
            return ResultVOUtils.fail500(null);
        }

    }

    /**
     * 对用户进行登陆判断，如果为true则继续判断密码是否正确，正确放回数值，错误返回300,账户不存在则返回404
     *
     * @param userName
     * @return
     */
    //通过用户名查询信息进行登录
    @GetMapping(value = "login/{userName}/{password}")
    public RestfulVO<User> UserLogin(@PathVariable String userName,
                                     @PathVariable String password) {

        Boolean str = userService.existsByUserName(userName);
        if (str == true) {
            //判断密码是否正确
            if (userService.existsByUserNameAndPassword(userName, password) == true) {
                User user = userService.findByUserName(userName);
                return ResultVOUtils.success(user);
            } else {
                return ResultVOUtils.fail300(null);
            }
        } else {
            return ResultVOUtils.fail404(null);
        }
    }

    //通过用户名修改信息
    @PostMapping(value = "edit")
    public RestfulVO<User> Edit(User user) {
        userService.update(user);
        //返回修改信息值
        return ResultVOUtils.recompose();
    }

    //同步用户名数据
    @GetMapping(value = "list/{userName}")
    public RestfulVO<User> list(@PathVariable String userName) {

        User user = userService.findByUserName(userName);
        return ResultVOUtils.synchronization(user);
    }

}
